var util = require('../../utils/util.js')
var app = getApp();
Page({
    data: {
        windowwidth: app.globalData.deviceInfo.windowWidth,//根据手机分辨率设置页面大小
        windowheight: app.globalData.deviceInfo.windowHeight,
        userN: '',
        passW: '',
        yanzhengCode: '',
        imagedata: '',
        isAlert: false,//属否弹出 弹出页面内容区
        showCount: false,//是否显示60秒后重试
        countDwonText: "60秒后重试",
        second: 60//时间 秒
    },
    onLoad: function (options) {
      console.log(options)
      console.log('登录页面')
        wx.setNavigationBarTitle({//动态设置当前页面的标题。
            title: " "
        });
    },
    onShow: function () {
        wx.setNavigationBarTitle({//动态设置当前页面的标题。
            title: " "
        });
    },

    //登录btn  单击时触发
    dologin: function (e) {
        var that = this
        if (!this.data.passW.length) {
            wx.showToast({
                title: '请输入您收到的验证码',
                duration: 2000
            }); return;
        }
        var mdata = {
            "phoneNo": this.data.userN,
            "validateCode": this.data.passW,
            "deviceToken": ""
        };





        //验证验证码c2mwebservice/api/v1/login
        util.postRequest("/c2mwebservice/api/v1/login", mdata, function (res) {//验证成功 ？ 跳转主页 | 弹出错误提示【手机号或验证码错误】
            app.globalData.mohuanUser = res.returnData;
            wx.setStorage({
                key: "userdata",
                data: res.returnData
            });
            wx.navigateBack();//跳转主页
        }, function (res) {
            wx.showToast({
                title: '手机号或验证码错误',
                duration: 2000
            })
        });
    },
    //发送验证码 btn  单击时触发 弹出页面
    dovalidate: function (e) {
        if (!this.data.userN.length) {
            wx.showToast({
                title: '请输入正确手机号码',
                duration: 2000
            }); return;
        }
        var that = this;
        //调接口显示验证码图片
        util.request("/c2mwebservice/api/v2/public/captcha/image-url/log", function (dataJson) {
            app.globalData.imageRequestId = dataJson.returnData.imageRequestId;
            that.setData({ imagedata: dataJson.returnData.url })
        }, function (reason) { });
        this.setData({//显示验证码弹出内容区
            isAlert: true
        });
    },
    //验证图片 确定按钮。 true会发送手机验证码
    getvalidate: function (e) {
        if (!this.data.yanzhengCode.length) {
            wx.showToast({
                title: '请输入验证码',
                duration: 2000
            });
            return;
        }
        var that = this;
        var mydata = {
            "emailPhone": this.data.userN,
            "idCode": '+86',
            "codePurpose": 'log',
            "imageCode": this.data.yanzhengCode
        };
        util.postRequest("/c2mwebservice/api/v1/public/registration", mydata, function (res) {
            that.setData({
                isAlert: false,
                showCount: true
            });
            that.countdown(that);
        }, function (reason) {
            that.setData({
                isAlert: false
            });
            wx.showToast({
                title: reason,
                duration: 2000
            })
        });
    },
    //更换验证码图片
    getagin: function () {
        if (!this.data.userN.length) {
            wx.showToast({
                title: '请输入正确手机号码',
                duration: 2000
            })
            return;
        }
        var that = this;
        util.request("/c2mwebservice/api/v2/public/captcha/image-url/log", function (dataJson) {
            app.globalData.imageRequestId = dataJson.returnData.imageRequestId;
            that.setData({
                imagedata: dataJson.returnData.url
            })

        }, function (reason) {

        });
    },

    //用户名和密码输入框事件
    userNameInput: function (e) {
        this.setData({
            userN: e.detail.value
        })
    },
    passWdInput: function (e) {
        this.setData({
            passW: e.detail.value
        })
    },
    validCodeInput: function (e) {
        this.setData({
            yanzhengCode: e.detail.value
        })
    },
    hideAlert: function (e) {//隐藏 验证码弹出内容区
        this.setData({
            isAlert: false
        });
    },
    goxieyi: function (e) {//跳转【用户注册协议页面】
        wx.navigateTo({
            url: '../webview/webview'
        });
    },


    countdown: function (that) {//60秒倒计时
        var second = that.data.second
        if (second == 0) {
            that.setData({
                showCount: false,
                second: 60,
                countDwonText: "60秒后重试",
            });
            clearTimeout(micro_timer);
            return;
        }
        var timer = setTimeout(function () {
            that.setData({
                second: second - 1,
                countDwonText: `${second}秒后重试`
            });
            that.countdown(that);
        }, 1000)
    },


    onShareAppMessage: function () {//分享功能
        return {
            title: 'Cotte',
            desc: '酷特智能'
        }
    }
})
